package com.gzhryc.shared_device.oem.h5.agent.mng.controllers.market.callback;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.api.DefaultAPICodeConstants;
import com.gzhryc.common.api.DefaultAPIResponse;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.net_marketing.core.enums.EGoodsAuditState;
import com.gzhryc.net_marketing.core.notice.goods.GoodsAuditResultNotice;
import com.gzhryc.net_marketing.supplier.Supplier;
import com.gzhryc.net_marketing.supplier.SupplierManager;
import com.gzhryc.net_marketing.supplier.erp.dao.enums.SupplierGoodsEnum;
import com.gzhryc.net_marketing.supplier.erp.services.SupplierGoodsService;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;

public class MarketSupplierNoticeController {
	
	static Logger log = Logger.getLogger(MarketSupplierNoticeController.class);

	/**
	 * 云商品审核
	 * @param request
	 * @return
	 */
	@Route(value = "/market/supplier/notice/{supplierId}/goods/audit/result")
    public DefaultAPIResponse auditResult(HttpServletRequest request) {
		DefaultAPIResponse result = new DefaultAPIResponse(DefaultAPICodeConstants.EXECUTE_ERROR,"处理失败");
		Long supplierId = WebUtils.getLong(request, "supplierId");
        Supplier supplier = SupplierManager.getSupplier(supplierId);
        if(supplier != null) {
        	GoodsAuditResultNotice noticeReq = WebUtils.getJsonRequest(request, GoodsAuditResultNotice.class);
        	if(noticeReq != null) {
        		log.info("云商品审核结果：{{0:json}}", noticeReq);
        		try {
        			Integer auditState = SupplierGoodsEnum.AuditState.Error.index();
        			if(EGoodsAuditState.Success.index().equals(noticeReq.getBody().getAuditState())) {
        				auditState = SupplierGoodsEnum.AuditState.Success.index();
        			}else if(EGoodsAuditState.Close.index().equals(noticeReq.getBody().getAuditState())) {
        				auditState = SupplierGoodsEnum.AuditState.Cancel.index();
        			}
        			
					if(SupplierGoodsService.self(supplier.getJdbcKey()).updateAuditResult(noticeReq.getBody().getGoodsId(), 
							noticeReq.getBody().getAuditId(), auditState, noticeReq.getBody().getGoodsCode(), noticeReq.getBody().getAuditNote())) {
						result.setCode(DefaultAPICodeConstants.SUCCESS, null);
					}
				} catch (LogicException e) {
					result.setMsg(e.getMessage());
				}
        		
        	}
        }
        return result;
    }
}
